package com.example.design.c.state.demo3;

/**
 * 线程运行状态
 */
public class StateRunning extends ThreadAbstractState {
    /**
     * 构造方法初始化状态名称
     */
    public StateRunning() {
        this.setStateName("运行");
    }

    @Override
    public boolean stop(ThreadContext context) {
        System.out.println("============================");
        System.out.println("线程终止...");
        String stateName = context.getState().getStateName();

        if ("运行".equals(stateName)) {
            context.setState(new StateDead());
            context.showState();
            return true;
        }
        System.out.println("非'运行'状态无法调用stop方法");
        return false;
    }

    // suspend 暂停 悬挂 吊起
    @Override
    public boolean suspend(ThreadContext context) {
        System.out.println("============================");
        System.out.println("挂起线程...");
        String stateName = context.getState().getStateName();

        if ("运行".equals(stateName)) {
            context.setState(new StateBlocked());
            context.showState();
            return true;
        }
        System.out.println("非'运行'状态无法调用suspend方法");
        return false;
    }
}
